<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
import { getUrl,confirmModal, ptFloat, minusF } from '@/static/js/common.js';
import { formValidation, tranferToEditors, changeEditor,setSubmitSubValue, configSelectData, hasValue,formShowLoading ,getFormFields,formModelLoad,getWorkCacheKey} from '@/static/js/formUtil.js';
import urlUtil from '@/static/js/urlUtil.js';
import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			checkedit:false,
			menuName: '',
			id: '',
			busCode: '',
			formdata: {
				payMoney:""
			},
			mainConfigData: [],
			subConfigData: {},
			initValue: {
				zinstId:"",
				zinstName:"",
				payMoney:"",
				objStr:""
			},
			subEditorObj: {}, 
			bankCacheKey:'fin-pay-applys-bank-cache',
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		onSubChange(){
			let _that = this.$refs.autoform
			if(this.subEditorObj["title2"].workList.length) {
				_that.formdata.payMoney = this._.sum(this.subEditorObj["title2"].workList.map(e=>ptFloat(e.payMoneyCode)))
			} else {
				_that.formdata.payMoney = 0
			}
			this.getCapitalPaySum()
		},
		getCapitalPaySum(){
			let _that = this.$refs.autoform;
			if(_that.formdata.payMoney){
				_that.om.request("common/getCapitalNum", {
					moneyNum: _that.formdata.payMoney
				}).then(res => {
					if (res.succeed) {
						_that.formdata.payMoneyCapital = res.value
					}
				})
			}else{
				_that.formdata.payMoneyCapital = ""
			}
		},
		formSubmit() {
			let _that = this;
			var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
			if (checkRes) {
				this.om.toast(checkRes);
			} else {
				//子表提交数据赋值
				setSubmitSubValue()
				if (!_that.formdata.objStr) {
					this.om.toast('请配置' + this.subEditorObj['title2'].title);
					return;
				}
				if(!_that.checkedit){
					_that.om.formSave('tpay/addTpay', _that.formdata, _that)
				}else{
					_that.om.formSave('commonForm/doCheckOrApproveUpdate', _that.formdata, _that)
				}
				
			}
		},
		getWorkParam() {
			return {
				checkedit:(this.checkedit?true:""),
			}
		},
		onSelectZpos(item){
			this.$refs.autoform.formdata.zinstId = item.instId
			this.$refs.autoform.formdata.zinstName = item.instName
		},
		onSelectPayType(item) {
			let _that = this;
			if(item.code == "37848"){
				_that.$refs.autoform.changeEditor("title1","payeeBankName","required",true)
				_that.$refs.autoform.changeEditor("title1","payeeBankNum","required",true)
			}else{
				_that.$refs.autoform.changeEditor("title1","payeeBankName","required",false)
				_that.$refs.autoform.changeEditor("title1","payeeBankNum","required",false)
			}
		},
		onSelectBank(item){
			this.$refs.autoform.formdata.bankId = item.id;
			this.$refs.autoform.formdata.bankName = item.dicname
			this.$refs.autoform.formdata.card = item.signcode
		},
		clearBank(){
			this.$refs.autoform.formdata.bankId = "";
			this.$refs.autoform.formdata.bankName = ""
			this.$refs.autoform.formdata.card = ""
		},
		initLoad(fileId) {
			let _that = this;
			if (!this.busCode) {
				return;
			}
			var ajaxArr = [urlUtil.zposUrl(this.busCode)];
			if(fileId){
				ajaxArr.push({
					url:'tpay/getInfo',
					param:{
						id:fileId
					},
					method:'post'
				})
			}
			
			formModelLoad(ajaxArr, function(res, mainData) {
				changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'),mainData);
				changeEditor("title1", "bankId","link","../../public/search/search",mainData)
				changeEditor("title1", "bankId", "linkParam", {
					other:JSON.stringify({
						code: "36614"
					}),
					namefield:"dicname",
					initload:true,
					url:"tsysdict/getCombobox",
					cachename:_that.bankCacheKey,
					placeholder:"请输入开户行"
				},mainData)
				changeEditor("title2", "objStr", {
					getWorkParam: 'getWorkParam',
					titleTemplate:" ¥ <%= payMoneyCode %> + <%= posName || '未填写'%>",
					subtitle1Template:"<%= userName + ' | ' %>",
					subtitle2Template:"<%= remark %>"
				}, null, mainData)
				if(fileId){
					let fieldArr = getFormFields(mainData)
					fieldArr.push('id')
					for(let index in res[1].value){
						if(fieldArr.includes(index)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
							_that.$set(_that.formdata,index,res[1].value[index])
						}
					}
					 let dataList = _that._.cloneDeep(res[1].value.list)
					 for(let item of dataList){
						 delete item.files
					 }
					 _that.subEditorObj['title2'].workList = dataList
					 _that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr',JSON.stringify(dataList))
					_that.$set(_that.formdata,'objStr',res[1].value.list)
					delete res[1].value.dataList
					
				}
				_that.$nextTick(()=>{
					if(!_that.$refs.autoform.formdata.zposId){
						_that.$refs.autoform.initSelectVal("topinfo","zposId")
					}else{
						_that.$refs.autoform.initSelectIndex("topinfo","zposId")
					}
				})
				
			})
		},
	},
	onLoad(options) {
		let _that = this;
		_that.menuName = options.title || '付款';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		formShowLoading(_that)
		_that.busCode = options.code || "FIN0405";
		_that.formdata.userId = this.userId;
		if(options.checkedit){
			if(typeof(options.checkedit) == 'string'){
				if(options.checkedit == "true"){
					_that.checkedit = true
				}
			}else{
				_that.checkedit = options.checkedit
			}
		}
		_that.initLoad(options.fileId);
		uni.removeStorage({
			key: _that.workCacheKey
		})
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				var editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						_that.onSubChange(editor)
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			uni.getStorage({
				key: _that.bankCacheKey,
				success: function(res) {
					_that.onSelectBank(res.data);
					uni.removeStorage({
						key: _that.bankCacheKey
					});
				}
			});
		}
	}
};
</script>
<style></style>
